package com.glxy.service.impl;

import com.glxy.controller.UserServlet;
import com.glxy.dao.impl.BillDaoImpl;
import com.glxy.dao.impl.UserDaoImpl;
import com.glxy.pojo.User;
import com.glxy.service.UserService;

import java.util.ArrayList;

public class UserServiceImpl implements UserService {

    @Override
    public Integer totalCount() {
        return new UserDaoImpl().findAll().size();
    }

    @Override
    public ArrayList<User> findAllUserPages(Integer pageSize, Integer pageNo) {
        ArrayList<User> list = new UserDaoImpl().findAll();
        ArrayList<User> page = new ArrayList<>();

        for (int i = pageSize*(pageNo - 1); i < pageSize*(pageNo - 1) + pageSize; i++) {
            if (i < list.size()) page.add(list.get(i));
        }

        return page;
    }

    public ArrayList<User> findUser(String userName){
        return new UserDaoImpl().findByName(userName);
    }



    @Override
    public ArrayList<User> getUserList() {
        UserDaoImpl userDao = new UserDaoImpl();
        return userDao.findAll();
    }

    @Override
    public void insertUser(User user) {
        UserDaoImpl userDao = new UserDaoImpl();
        userDao.insert(user);
    }

    public void update(User user) {
        UserDaoImpl userDao = new UserDaoImpl();
        userDao.update(user);
    }


    public void delete(Long id){
        UserDaoImpl userDao = new UserDaoImpl();
        userDao.delete(id);
    }
}
